package com.library_manage_system.strategy;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import com.library_manage_system.view.WindowsMainImpl;
import com.library_manager_system.Book;
import com.library_manager_system.utils.Table;

public class FindBookPanelStrategy implements PanelStrategy{

	private JPanel jtablePanel;
	private CardLayout cardLayout;
	private JPanel findBookPanel;
	
	@Override
	public JPanel createPanel(final ArrayList<Book> arraylist,WindowsMainImpl windowsMainImpl) {
		// TODO Auto-generated method stub
			findBookPanel = new JPanel();
			cardLayout = new CardLayout();
			findBookPanel.setLayout(cardLayout);

			// 设置一个jlabel来提示用户输入要查找的书的信息
			JLabel jlabel = new JLabel("请在下面输入你想要借的书的首个字,如果不输入则显示全部");

			// 设置一个文本框来让客户输入要查找的书的关键字
			final JTextField jTextField = new JTextField(20);

			// 设置一个按钮表示提交想要借的书
			JButton jButton = new JButton("确定查找");

			// jButton.setPreferredSize(new Dimension(50,30));
			// jButton.setLocation(50, 50);

			JPanel jPanel = new JPanel();
			jPanel.setLayout(new GridLayout(3, 1, 50, 50));
			jPanel.add(jlabel);
			jPanel.add(jTextField);
			jPanel.add(jButton);
			JPanel jp = new JPanel();
			jp.add(jPanel);

			jtablePanel = new JPanel();

			findBookPanel.add(jp, "jp");

			jButton.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					// 定义一个暂时的书数组，用来存放这些首字母相同的书
					ArrayList<Book> list = new ArrayList<Book>();
					// 获取用户输入的书名
					String s = jTextField.getText().trim();
					if (s.equals("")) {

						createTable(arraylist);
					} else {
						// 获取用户输入的书名的首字
						String head = getHead(s);
						// 判断是否有书名跟首字母相同，如果有，就输出
						Iterator<Book> it = arraylist.iterator();

						// 定义一个temp，来记录有几本书的首字母相同
						int temp = 0;
						while (it.hasNext()) {
							Book book = it.next();
							int i = -1;
							// 如果查不到就返回-1
							if (head.equals(getHead(book.getBook_name()))
									|| (i != (book.getBook_name().indexOf(s)))) {
								list.add(book);
								temp++;
							}
						}
						// 建立一个表格
						// CellData:行 columNames:列
						createTable(list);
					}
				}
			});

			return findBookPanel;
		}

		public String getHead(String s) {

			byte[] b = s.getBytes();
			String s1 = "";
			if (b[0] < 0) {
				byte[] b1 = { b[0], b[1] };
				s1 = b1.toString();
			} else {

				return s;
			}

			return s1;
		}

		public void createTable(ArrayList<Book> list) {

			JTable jtable = Table.createTable(list);
			
			JScrollPane jScrollPane = new JScrollPane(jtable);
			// 设置奇数行和偶数行的颜色
			Table.makeFace(jtable);

			jtablePanel = new JPanel();
			jtablePanel.setLayout(new BorderLayout());
			JPanel jbuttonPanel = new JPanel();
			JButton jButton2 = new JButton("返回");
			jbuttonPanel.add(jButton2);
			
			jtablePanel.add(jtable.getTableHeader(), BorderLayout.NORTH);
			jtablePanel.add(jScrollPane, BorderLayout.CENTER);
			jtablePanel.add(jbuttonPanel, BorderLayout.SOUTH);
			
			findBookPanel.add(jtablePanel, "jtable");
			cardLayout.show(findBookPanel, "jtable");

			jButton2.addActionListener(new ActionListener() {

				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					cardLayout.show(findBookPanel, "jp");
				}
			});
		}
		

}
